package org.eyeTopLight.service.impl;

import org.eyeTopLight.dao.LenGoodsDao;
import org.eyeTopLight.po.LenGoods;
import org.eyeTopLight.exception.ErrorCode;
import org.eyeTopLight.exception.ExceptionHandler;
import org.eyeTopLight.service.LenGoodsService;
import org.eyeTopLight.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class LenGoodsServiceImpl implements LenGoodsService {

    public static final Logger logger = LoggerFactory.getLogger(LenGoodsServiceImpl.class);

    @Resource
    LenGoodsDao lenGoodsDao;

    @Override
    public LenGoods getGoodsByPK(Integer goods_id) {

        LenGoods searchGoods = new LenGoods();
        searchGoods.setLenGoods_id(goods_id);

        LenGoods goods = null;
        try {
            goods = lenGoodsDao.queryByPK(searchGoods);
        }catch (Exception e){
            logger.error("查询数据库lenGoods表操作失败",e);
            ExceptionHandler.processError(ErrorCode.ERR_DB_SELECT.getValue(),"查询数据库lenGoods表操作失败");
        }
        return goods;

    }


    @Override
    public List<GoodsVo> getGoods(String goods_category, Integer startIndex, Integer maxRows) {

        LenGoods searchGoods = new LenGoods();
        if(!goods_category.equals("全部")){
            searchGoods.setLenGoods_category(goods_category);
        }


        List<GoodsVo> goodsList = new LinkedList<>();
        try{
            goodsList = lenGoodsDao.queryForPage(searchGoods,startIndex,maxRows);
        }
        catch (Exception e){
            logger.error("查询数据库lenGoods表操作失败",e);
            ExceptionHandler.processError(ErrorCode.ERR_DB_SELECT.getValue(),"查询数据库lenGoods表操作失败");
        }
        return goodsList;
    }

    @Override
    public LenGoods getOneGood(String goods_symbol, Integer goods_id) {
        LenGoods lenGoods = null;

        LenGoods searchGoods = new LenGoods();
        searchGoods.setLenGoods_id(goods_id);
        try{
            lenGoods = lenGoodsDao.queryByPK(searchGoods);
        }
        catch (Exception e){
            logger.error("查询数据库lenGoods表操作失败",e);
            ExceptionHandler.processError(ErrorCode.ERR_DB_SELECT.getValue(),"查询数据库lenGoods表操作失败");
        }
        return lenGoods;
    }

    @Override
    public List<GoodsVo> getTopNGoods(Integer count) {
        List<GoodsVo> topNFrames = lenGoodsDao.queryTopN(count);
        return topNFrames;
    }
}
